package yunhe.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;

import yunhe.biz.StockMgrBiz;
import yunhe.biz.impl.StockMgrBizImpl;
import yunhe.entity.Storage;
import yunhe.util.GeneralUtil;

@WebServlet(name = "GetAllStorServlet", urlPatterns = "/GetAllStorServlet")
public class GetAllStorServlet extends HttpServlet

{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    	HttpSession session=req.getSession();
    	
    	String Takcode=GeneralUtil.creatSeq("PD");
    	session.setAttribute("Takcode",Takcode);
    	
        StockMgrBiz stock = new StockMgrBizImpl();
        //查出所有的仓库
        List<Storage> list_storage = stock.querySto();
        //将仓库列表设置到request中
        String ajax=req.getParameter("ajax");
        if(ajax!=null&&ajax.equals("comb")){
            JSONArray ja=new JSONArray();
            for (Storage s:list_storage){
                Map<String,String> mss=new HashMap<>();
                mss.put("id",s.getStoId());
                mss.put("text",s.getStoName());
                ja.put(mss);

            }
            System.out.println(ja.toString());
            resp.getWriter().print(ja.toString());
        }
        else{
            req.setAttribute("stoList", list_storage);
            req.getRequestDispatcher("article-list.jsp").forward(req, resp);
        }
    }

}
